 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.view
 * File     : ListOptionDialog.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : Nov 20, 2011
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

/**
 *  
 *  方便处理需要在ListView的item上长按出现的AlertDialog
 *
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class ListOptionDialog implements DialogInterface.OnClickListener{
	private AlertDialog mDialog;
	private ListView mListView;
	private int mPosition;
	private int mChild;
	private String mTitle;
	private Context mContext;
	private List<ListOption> mOptions;
	private OnListOptionClickListener mListener;
	
	public ListOptionDialog(Context context, ListView listView, 
			int position,int child, OnListOptionClickListener listener){
		mContext   = context;
		mListView  = listView;
		mPosition  = position;
		mChild     = child;
		mOptions   = new ArrayList<ListOption>();
		mListener  = listener; 
	}
	
	public ListOptionDialog(Context context, ListView listView, 
			int position, OnListOptionClickListener listener){
		this(context, listView, position, -1, listener);
	}
	
	public ListOptionDialog show(){
		Iterator<ListOption> it = mOptions.iterator();
		String[] items = new String[mOptions.size()];
		int index = 0;
		while(it.hasNext()){
			ListOption o = it.next();
			items[index++] = o.getText();
		}
		
		
		mDialog = new AlertDialog.Builder(mContext)
		.setTitle(mTitle)
		.setItems(items, this)
		.create();
		mDialog.show();
		return this;
	}
	
	public void dismiss(){
		mDialog.dismiss();
	}
	
	public ListOptionDialog setTitle(String title){
		mTitle = title;
		return this;
	}
	
	public ListOptionDialog setListOptions(List<ListOption> options){
		mOptions = options;
		return this;
	}
	
	public ListOptionDialog addListOption(int id, String text){
		mOptions.add(new ListOption(id, text, null));
		return this;
	}
	
	public ListOptionDialog addListOption(int id, String text, Object tag){
		mOptions.add(new ListOption(id, text, tag));
		return this;
	}
	
	public AlertDialog getDialog() {
		return mDialog;
	}

	public View getListView() {
		return mListView;
	}

	public Object getItemData() {
		if(mChild==-1){
			return mListView.getItemAtPosition(mPosition);
		}else {
			ExpandableListView lv = (ExpandableListView) mListView;
			return lv.getExpandableListAdapter().getChild(mPosition, mChild);
		}
	}

	public List<ListOption> getmOptions() {
		return mOptions;
	}

	public static interface OnListOptionClickListener{
		public void onClick(ListOptionDialog dialog, ListOption option);
	}
	public static class ListOption{
		private int mId;
		private String mText;
		private Object mTag;
		public ListOption(int mId, String mText, Object mTag) {
			this.mId = mId;
			this.mText = mText;
			this.mTag = mTag;
		}
		public int getId() {
			return mId;
		}
		public String getText() {
			return mText;
		}
		public Object getTag(){
			return mTag;
		}
	}
	@Override
	public void onClick(DialogInterface dialog, int which) {
		if(mListener!=null){
			mListener.onClick(this, mOptions.get(which));
		}
	}
}
